package com.zjk.product.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.zjk.product.config.FastDFSClient;
import com.zjk.result.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: gfk
 * @create: 2022/11/3
 * @Description:
 * @FileName: UploadController
 * @History:
 * @自定义内容：
 */
@RestController
@RequestMapping("product")
public class UploadController {

    @Autowired
    OSS ossClient;

    @Autowired
    FastDFSClient fastDFSClient;

    @Value("${oss.bucket}")
    String bucketName;
    @Value("${oss.hostName}")
    String hostName;

    @PostMapping("uploadOss")
    public ResultResponse uploadOss(@RequestBody MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();

        PutObjectResult putObjectResult = ossClient.putObject(bucketName, originalFilename, file.getInputStream());

        String url=hostName=originalFilename;

        return new ResultResponse(true,"上传成功",url);
    }
    @PostMapping("uploadFastDFS")
    public ResultResponse uploadFastDFS(@RequestBody MultipartFile file) throws IOException {
        String url = fastDFSClient.upload(file);

        return new ResultResponse(true,"上传成功",url);
    }


}
